﻿using Leeder.Logger;
using ScanConfirm.Business;
using ScanConfirm.Handle;
using ScanConfirm.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScanConfirmForm
{
    public partial class FrmSetup : Form
    {
        ScanConfirmHandler scanConfirmHandler;
        PartsSetupBLL partsSetupBLL = new PartsSetupBLL();
        PartsCodeEquateBLL partsCodeEquateBLL = new PartsCodeEquateBLL();
        QueueInfoBLL queueInfoBLL = new QueueInfoBLL();
        SysSetupModel sysSetupModel;
        private LogOperate LogObj => Log.GetLogger(this.GetType());
        CarTypeBLL carTypeBLL = new CarTypeBLL();
        int pageSize = 30;       //数据表每页默认行数
        public FrmSetup()
        {
            InitializeComponent();
            //全屏，覆盖任务栏
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }

            dgvCarTyp.ClearSelection();
            dgvCarTyp.AutoGenerateColumns = false;
            dgvCarTyp.ReadOnly = true;
            dgvCarTyp.AddColumn("车型名称", "CarName");
            dgvCarTyp.AddColumn("车型代码", "CarCode");

            dgvParts.ClearSelection();
            dgvParts.AutoGenerateColumns = false;
            dgvParts.ReadOnly = true;
            dgvParts.AddColumn("车型代码", "CarCode");
            dgvParts.AddColumn("品番", "PartsCode");
            dgvParts.AddColumn("记号", "Mark");
            dgvParts.AddColumn("显示记号", "DisplayCode");
            dgvParts.AddColumn("程序号", "Code");

            dgvPartsEquate.ClearSelection();
            dgvPartsEquate.AutoGenerateColumns = false;
            dgvPartsEquate.ReadOnly = true;
            dgvPartsEquate.AddColumn("品番/记号", "DisplayCode");
            dgvPartsEquate.AddColumn("品番等同", "PartsCodeEquate");

            dgvLocation.ClearSelection();
            dgvLocation.AutoGenerateColumns = false;
            //dgvLocation.ReadOnly = true;
            dgvLocation.AddColumn("车型", "VehicleModel");
            dgvLocation.AddColumn("连番", "Sequence");
            dgvLocation.AddColumn("VIN码", "VIN");
            dgvLocation.AddColumn("品番", "PartsCode");
            dgvLocation.AddColumn("显示记号", "DisplayCode");
            dgvLocation.AddColumn("下发时间", "CreatedDate");
            scanConfirmHandler = ScanConfirmHandler.getInstance();

            //设置开始时间为三天前
            this.dpStartDate.Value = DateTime.Now.AddDays(-3);
            this.dpEndDate.Value = DateTime.Now;

            //设置分页控件每页数量
            uiPagination1.PageSize = pageSize;
            uiPagination1.Style = UIStyle.Blue;
        }

        private void FrmSetup_Load(object sender, EventArgs e)
        {
            var carModelList = carTypeBLL.GetModelAll();
            if (carModelList == null)
                carModelList = new List<CarTypeModel>();
            carModelList.Insert(0, new CarTypeModel { CarName = "--ALL--", CarCode = "--ALL--" });
            cbVehicleModel.DataSource = carModelList;
            cbVehicleModel.DisplayMember = "CarName";
            cbVehicleModel.ValueMember = "CarName";
            cbVehicleModel.SelectedIndex = 0;

            cbCarCode.DataSource = carModelList;
            cbCarCode.DisplayMember = "CarCode";
            cbCarCode.ValueMember = "CarCode";
            cbCarCode.SelectedIndex = 0;

            Search_Location();
            Search_CarType();
            Search_Parts();
            Search_PartsEquate();

            sysSetupModel = scanConfirmHandler.GetSysSetup();

            txtQueueLength.Text = sysSetupModel.QueueCount.ToString();

            if (sysSetupModel.InputType == "扫码枪")
                rbgInputType.SelectedIndex = 0;
            else if (sysSetupModel.InputType == "PDA")
                rbgInputType.SelectedIndex = 1;

            if (sysSetupModel.ProductionGuideType == "POKAYOKE")
                rpgProductionGuideType.SelectedIndex = 0;
            else if (sysSetupModel.ProductionGuideType == "TRIA")
                rpgProductionGuideType.SelectedIndex = 1;

            if (sysSetupModel.CheckType == "Mark")
                rpgCheckType.SelectedIndex = 0;
            else if (sysSetupModel.CheckType == "PartsCode")
                rpgCheckType.SelectedIndex = 1;
        }


        /// <summary>
        /// 查询--队列定位
        /// </summary>
        private void Search_Location()
        {
            DateTime? startDate = dpStartDate.Value;
            DateTime? endDate = dpEndDate.Value;
            ////设置分页控件总数
            uiPagination1.TotalCount = queueInfoBLL.GetModelCountByWhere(cbVehicleModel.SelectedValue.ToString(), txtSequence.Text, txtVIN.Text, startDate, endDate);
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        /// <summary>
        /// 查询--车型
        /// </summary>
        private void Search_CarType()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                dgvCarTyp.DataSource = null;
                dgvCarTyp.DataSource = carTypeBLL.GetModelListByCondition(txtCarName.Text.Trim(), txtCarCode.Text.Trim());
            }));
        }

        /// <summary>
        /// 查询--部品配置
        /// </summary>
        private void Search_Parts()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                dgvParts.DataSource = null;
                dgvParts.DataSource = partsSetupBLL.GetModelListByCondition(cbVehicleModel.SelectedValue.ToString(),txtPartsCode.Text,txtMark.Text);
            }));
        }

        /// <summary>
        /// 查询--品番等同
        /// </summary>
        private void Search_PartsEquate()
        {
            this.Invoke(new MethodInvoker(() =>
            {
                dgvPartsEquate.DataSource = null;
                dgvPartsEquate.DataSource = partsCodeEquateBLL.GetModelListByCondition(txtCode.Text);
            }));
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            try
            {
                DateTime? startDate = dpStartDate.Value;
                DateTime? endDate = dpEndDate.Value;
                dgvLocation.DataSource = queueInfoBLL.GetModelListByWhere(cbVehicleModel.SelectedValue?.ToString(), txtSequence.Text, txtVIN.Text, startDate, endDate, pageIndex, count);
                dgvLocation.Refresh();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Search_Location();
        }

        private void btnLocation_Click(object sender, EventArgs e)
        {
            var model = dgvLocation.CurrentRow.DataBoundItem as QueueInfoModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要定位的行！");
                return;
            }else
            {
                MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("是否确定要定位到连番"+ model.Sequence+"的队列？", "提示", mess);
                if (dr == DialogResult.OK)
                {
                    scanConfirmHandler.SetLocation(model, scanConfirmHandler.SysSetup.QueueCount);
                    //Search_Location();
                    this.Close();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            sysSetupModel.QueueCount = int.Parse(txtQueueLength.Text);

            if (rbgInputType.SelectedIndex == 0)
                sysSetupModel.InputType = "扫码枪";
            else if (rbgInputType.SelectedIndex == 1)
                sysSetupModel.InputType = "PDA";

            if (rpgProductionGuideType.SelectedIndex == 0)
                sysSetupModel.ProductionGuideType = "POKAYOKE";
            else if (rpgProductionGuideType.SelectedIndex == 1)
                sysSetupModel.ProductionGuideType = "TRIA";

            if (rpgCheckType.SelectedIndex == 0)
                sysSetupModel.CheckType = "Mark";
            else if (rpgCheckType.SelectedIndex == 1)
                sysSetupModel.CheckType = "PartsCode";

            sysSetupModel.SyncFlag = 2;

            var result = scanConfirmHandler.SetSysSetup(sysSetupModel);
            if(result)
            {
                MessageBox.Show("保存成功！");
            }else
            {
                MessageBox.Show("保存失败！");
            }

            //scanConfirmHandler.SaveConfigTest();
        }

        private void rbgInputType_ValueChanged(object sender, int index, string text)
        {

        }

        private void rpgProductionGuideType_ValueChanged(object sender, int index, string text)
        {

        }

        private void btnCarType_Search_Click(object sender, EventArgs e)
        {
            Search_CarType();
        }

        private void btnCarType_Add_Click(object sender, EventArgs e)
        {
            FrmCarTypeSet frm = new FrmCarTypeSet();
            frm.RefreshUiEvent += Search_CarType;
            frm.Show();
        }

        private void btnCarType_Update_Click(object sender, EventArgs e)
        {
            var model = dgvCarTyp.CurrentRow.DataBoundItem as CarTypeModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要修改的车型！");
                return;
            }
            else
            {
                FrmCarTypeSet frm = new FrmCarTypeSet(model);
                frm.RefreshUiEvent += Search_CarType;
                frm.Show();
            }
        }

        private void btnCarType_Del_Click(object sender, EventArgs e)
        {
            var model = dgvCarTyp.CurrentRow.DataBoundItem as CarTypeModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要删除的车型！");
                return;
            }
            else
            {
                MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("是否删除车型" + model.CarName + "吗？", "提示", mess);
                if (dr == DialogResult.OK)
                {
                    carTypeBLL.Delete(model);
                    Search_CarType();
                }
            }
        }

        private void btnPartSet_Search_Click(object sender, EventArgs e)
        {
            Search_Parts();
        }

        private void btnPartSet_Add_Click(object sender, EventArgs e)
        {
            FrmPartSet frm = new FrmPartSet();
            frm.RefreshUiEvent += Search_Parts;
            frm.Show();
        }

        private void btnPartSet_Update_Click(object sender, EventArgs e)
        {
            var model = dgvParts.CurrentRow.DataBoundItem as PartsSetupModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要修改的部品配置！");
                return;
            }
            else
            {
                FrmPartSet frm = new FrmPartSet(model);
                frm.RefreshUiEvent += Search_Parts;
                frm.Show();
            }
        }

        private void btnPartSet_Del_Click(object sender, EventArgs e)
        {
            var model = dgvParts.CurrentRow.DataBoundItem as PartsSetupModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要删除的部品配置！");
                return;
            }
            else
            {
                MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("是否删除该部品配置吗？", "提示", mess);
                if (dr == DialogResult.OK)
                {
                    partsSetupBLL.Delete(model);
                    Search_Parts();
                }
            }
        }

        private void btnPartsEquate_Add_Click(object sender, EventArgs e)
        {
            FrmPartsEquateSet frm = new FrmPartsEquateSet();
            frm.RefreshUiEvent += Search_PartsEquate;
            frm.Show();
        }

        private void btnPartsEquate_Update_Click(object sender, EventArgs e)
        {
            var model = dgvPartsEquate.CurrentRow.DataBoundItem as PartsCodeEquateModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要修改的品番等同配置！");
                return;
            }
            else
            {
                FrmPartsEquateSet frm = new FrmPartsEquateSet(model);
                frm.RefreshUiEvent += Search_PartsEquate;
                frm.Show();
            }
        }

        private void btnPartsEquate_Search_Click(object sender, EventArgs e)
        {
            Search_PartsEquate();
        }

        private void btnPartsEquate_Delete_Click(object sender, EventArgs e)
        {
            var model = dgvPartsEquate.CurrentRow.DataBoundItem as PartsCodeEquateModel;
            if (model == null)
            {
                MessageBox.Show("请先选择要删除的品番等同！");
                return;
            }
            else
            {
                MessageBoxButtons mess = MessageBoxButtons.OKCancel;
                DialogResult dr = MessageBox.Show("是否删除该品番等同吗？", "提示", mess);
                if (dr == DialogResult.OK)
                {
                    partsCodeEquateBLL.Delete(model);
                    Search_PartsEquate();
                }
            }
        }

        private void btnPartsEquate_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
